<?php

namespace app\index\controller;

use app\common\controller\Basecontroller;
use app\common\controller\OssStorage;
use app\index\model\Coenvironment;
use app\index\model\Company;
use app\index\model\Coproduct;
use app\index\model\Cotype;
use app\index\model\Examine;
use app\index\model\Official;
use app\index\model\Paf;
use app\index\model\User;
use app\validate\Vali;
use think\Db;
use think\Request;
use think\Validate;

class Boss extends Basecontroller {
    /**显示姓名，头像，应用company模型中的方法，返回相应的code
     * @copyright hyw (接口未完善)
     * @return \think\response\Json
     */
    public function logo(Company $company,User $user) {
        $user_id = $this->getUserID();
        $company_id = json_decode($user->getComiDAttr($user_id))->company_id;
        return $this->returnJson(self::OK,'输出成功',$company->getLogoAttr($company_id));
    }

    /**
     * 显示人事信息接口
     * @copyright hyw
     * @param User $user
     * @return \think\response\Json
     */
    public function MatterIntr(User $user) {
        $user_id = $this->getUserID();
        return $this->returnJson(self::OK,'输出成功',$user->getMatterLogoAttr($user_id));
    }

    /**显示公司的信息接口：通过查询方法返回数据并返回相应的code
     * @copyright hyw (接口未完善)
     * @param Request $request
     * @return \think\response\Json
     */
    public function companyInfr(Company $company,User $user) {
        $user_id = $this->getUserID();
        $company_id = json_decode($user->getComiDAttr($user_id))->company_id;
        return $this->returnJson(self::OK,'成功输出数据',$company->getInftAttr($company_id));
    }

    /**
     * 修改公司信息接口：获取上传数据，判断是否存在，存入数组，再进行修改并返回相应的code
     * @copyright hyw (未完善)
     * @param Request $request
     * @return \think\response\Json
     */
    public function infrChange(Request $request,Company $company,Cotype $ctype,User $user,Vali $vali) {
        $user_id = $this->getUserID();
        $company_id = json_decode($user->getComiDAttr($user_id))->company_id;
        $dataCom = [];
        $dataType = [];
        $comName = $request->param('comName');
        $comType = $request->param('comType');
        $comNum = $request->param('comNum');
        $comIntr = $request->param('comIntr');
        if (!isset($comName)&&!isset($comType)&&!isset($comNum)&&!isset($comIntr)) return $this->returnJson(self::ERROR,'无参数上传');
        $dataCom = [
            'company_name'               =>      $comName,
            'com_official'                      =>      $comNum,
            'company_introduction'   => $comIntr
        ];
        $dataType = [
            'type'                                   =>      $comType
        ];
        if (!$vali->scene('companyIntr')->batch()->check($dataCom)) return $this->returnJson(self::ERROR,'上传数据有误',$vali->getError());
        if (!$vali->scene('typeCheck')->batch()->check($dataType)) return $this->returnJson(self::ERROR,'上传数据有误',$vali->getError());
        $company->where('company_id','=',$company_id)->update($dataCom);
        $ctype->where('company_id','=',$company_id)->update($dataType);
        return $this->returnJson(self::OK,'修改数据成功',[$dataCom,$dataType]);
    }

    /**
     * 显示公司的产品信息接口：通过查询方法返回数据并返回相应的code,
     * 若通过则存入数据库并返回相应code
     * @copyright hyw (未完善)
     * @param Request $request
     * @return Coproduct|array|\PDOStatement|string|\think\Model|\think\response\Json|null
     */
    public function companyProduct(Coproduct $cop,User $user) {
        $user_id = $this->getUserID();
        $company_id = json_decode($user->getComiDAttr($user_id))->company_id;
        return $this->returnJson(self::OK,'成功输出数据',$cop->getProductAttr($company_id));
    }

    /**
     * 修改公司产品信息接口：获取上传数据，判断是否存在，存入数组，再进行修改并返回相应的code
     * @copyright hyw (未完善)
     * @param Request $request
     * @return \think\response\Json
     */
    public function productChange(Request $request,User $user,Coproduct $cop,Vali $vali,OssStorage $oss) {
        $user_id = $this->getUserID();
        $company_id = json_decode($user->getComiDAttr($user_id))->company_id;
        $file = $request->file('product_img');
        $coproduct = $request->param('coproduct');
        $productype = $request->param('productype');
        $product_introduction = $request->param('product_introduction');
        if (!isset($file)&&!isset($coproduct)&&!isset($product_introduction)&&!isset($productype)) return $this->returnJson(self::ERROR,'无参数上传');
        $allDate = [];
        $fileDate = [];
        for ($i = 0;$i < count($coproduct);$i++) {
            if (!$file[$i]->validate(['ext' => 'jpg,png,jpeg','size' => 2097152 ])->check()) return $this->returnJson(self::ERROR,'上传文件的格式错误或大小超过2M');
            $getFile = $oss->fileHandle($file[$i]);
            $url = $oss->upload($getFile['dst'],$getFile['src']);
            array_push($fileDate,$url);
            $data = [
                'product_img' => $fileDate[$i],
                'coproduct' => $coproduct[$i],
                'product_introduction' => $product_introduction[$i],
                'productType' => $productype[$i],
                'company_id' => $company_id
            ];
            if (!$vali->scene('bossProduct')->batch()->check($data))  return $this->returnJson(self::ERROR,'上传数据有错误',$vali->getError());
            array_push($allDate,$data);
        }
        $cop->insertAll($allDate);
        return $this->returnJson(self::OK,'上传成功',$allDate);
    }

    /**
     * 上传公司环境图片接口：接口上传文件，验证图片格式和大小，若验证不通过则返回错误相应的code，
     * 若通过存入相应路径，获取存入路径的文件名，存入数据库，并返回相应的code
     * @copyright hyw (接口未完善)
     * @param Request $request
     * @return \think\response\Json
     */
    public function postComImg(Request $request,Coenvironment $coe,User $user,OssStorage $oss,Vali $vali) {
        $file = $request->file('image');
        if (!isset($file)) return $this->returnJson(self::ERROR,'无参数上传');
        $redata = array();
        $userId = $this->getUserID();
        $comId = $user->getComiDAttr($userId)->company_id;
        foreach ($file as  $value) {
            if (!$value->validate(['ext' => 'jpg,png,jpeg','size' => 2097152 ])->check())  return $this->returnJson(self::ERROR,'上传格式或大小超过2M',$vali->getError());
            $getFile = $oss->fileHandle($value);
            $url = $oss->upload($getFile['dst'],$getFile['src']);
            $data =['coenviron_img' => $url,'company_id' => $comId];
            array_push($redata,$data);
        }
         $coe->insertAll($redata);
         return $this->returnJson(self::OK,'上传成功',$redata);
    }

    /**
     * 显示公司环境接口：从公司环境表的模型中的查询接口中输出，返回相应的code
     * @copyright hyw (接口未完善)
     */
    public function ComImg(Coenvironment $coe,User $user) {
        $user_id = $this->getUserID();
        $company_id = json_decode($user->getComiDAttr($user_id))->company_id;
        return $this->returnJson(self::OK,'输出数据成功',$coe->getImgAttr($company_id));
    }

    /**
     *公司福利接口：获取当前company_id，获取上传信息，将上传的数据存入数据库，并返回成功的code
     * @copyright hyw (接口未完善)
     * @param Request $request
     * @return Company|array|\PDOStatement|string|\think\Model|\think\response\Json|null
     */
    public function welfare(Company $com,User $user) {
        $user_id = $this->getUserID();
        $company_id = json_decode($user->getComiDAttr($user_id))->company_id;
        return $this->returnJson(self::OK,'成功输出',$com->getWelfAttr($company_id));
    }

    /**
     * 编辑公司福利接口：获取上传的值，修改数据库的值
     * @copyright hyw
     * @param Request $request
     * @param Company $com
     * @param User $user
     * @param Vali $vali
     * @return \think\response\Json
     */
    public function welfareChange(Request $request,Company $com,User $user,Vali $vali) {
        $user_id = $this->getUserID();
        $company_id = json_decode($user->getComiDAttr($user_id))->company_id;
        $welfare = $request->param('welfare');
        $welfDate = array();
        $str = '';
        if (!isset($welfare)) return $this->returnJson(self::ERROR,'上传为空');
        foreach ($welfare as $key => $value) {
            $str .= $value.',';
        }
        $str = substr($str,0,strlen($str)-1);
        $data = ['company_welfare' => $str];
        if (!$vali->scene('welfareChange')->check($data)) return $this->returnJson(self::ERROR,'上传格式有误',$vali->getError());
        $com->where('company_id','=',$company_id)->update($data);
        return $this->returnJson(self::OK,'修改成功',$data);
    }

    /**
     * 显示已聘用人事接口：引入用户表的查询方法，输出相应的code
     * @copyright hyw (接口未完善)
     * @return User[]|array|bool|\PDOStatement|string|\think\Collection|\think\model\Collection
     */
    public function matter(User $user) {
        $user_id = $this->getUserID();
        $company_id = json_decode($user->getComiDAttr($user_id))->company_id;
        return  $this->returnJson(self::OK,'输出数据成功',$user->getMatterAttr($company_id));
    }

    /**显示已发布职位接口：获取user表查询到的公司id，传到职位表的查询方法中返回相应的code
     * @copyright hyw (接口未完善)
     * @return \think\response\Json
     */
    public function official(Official $official,User $user) {
        $user_id = $this->getUserID();
        $company_id = json_decode($user->getComiDAttr($user_id))->company_id;
        return $this->returnJson(self::OK,'成功输出数据',$official->getOffAttr($company_id));
    }

    /**删除职位接口：获取到上传的职位id，获取user表查询到的公司id,判断公司id以及职位的id，再从数据库修改
     * @copyright hyw
     * @param Request $request
     * @return \think\response\Json
     */
    public function change(Request $request,Official $official,User $user) {
        $off= $request->param('off');
        $job_money = $request->param('jobMoney');
        $user_id = $this->getUserID();
        $company_id = json_decode($user->getComiDAttr($user_id))->company_id;
        $data = ['is_delete' => 1];
        $offId = $official->getOffIdAttr($company_id,$off,$job_money)->official_id;
        $official->where('official_id','=',$offId)->update($data);
        return $this->returnJson(self::OK,'删除成功');
    }

    /**
     * 已聘用职位接口
     * @copyright hyw
     * @param Examine $em
     * @param User $user
     * @return \think\response\Json
     */
    public function employPosition(Examine $em,User $user) {
        $user_id = $this->getUserID();
        $company_id = json_decode($user->getComiDAttr($user_id))->company_id;
        return $this->returnJson(self::OK,'输出成功',$em->getEmployAttr($company_id));

    }

    /**显示已删除职位接口：获取user表查询到的公司id，传入职位表的查询方法中，返回相应code
     * @copyright hyw (接口未完善)
     * @return \think\response\Json
     */
    public function deLe(Official $official,User $user) {
        $user_id = $this->getUserID();
        $company_id = json_decode($user->getComiDAttr($user_id))->company_id;
        return $this->returnJson(self::OK,'成功输出数据',$official->getOffDAttr($company_id));
    }

    /**
     * 修改头像接口：接口上传文件，验证图片格式和大小，若验证不通过则返回错误相应的code，
     * 若通过存入相应路径，获取存入路径的文件名，存入数据库，并返回相应的code
     * @copyright hyw (接口未完善)
     * @param Request $request
     * @return \think\response\Json
     */
    public function headImg(Request $request,User $user,OssStorage $oss,Vali $vali) {
        $user_id = $this->getUserID();
        $file = $request->file('headimg');
        if (!$file->validate(['ext' => 'jpg,png,jpeg','size' => 2097152])->check()) return $this->returnJson(self::ERROR,'上传格式或大小超过2M');
        $getFile = $oss->fileHandle($file);
        $url = $oss->upload($getFile['dst'],$getFile['src']);
        $data = ['header' => $url];
        $user->where('user_id','=',$user_id)->update($data);
        return $this->returnJson(self::OK,'修改头像成功',$data);
    }

    /**
     * 显示应聘人事的用户信息接口：调用方法，查询，返回成功的code
     * @copyright hyw
     * @return \think\response\Json
     */
    public function matterCheck(Paf $paf,User $user) {
        $user_id = $this->getUserID();
        return $this->returnJson(self::OK,'成功输出数据',$paf->getMaAttr($user_id));
    }

    /**
     *人事申请接口：接收上传数据，对相关的值进行修改，返回相关的code
     * @copyright hyw
     * @param Request $request
     * @return \think\response\Json
     */
    public function matterOr(Request $request,Paf $paf,User $user) {
        $user_id = $request->param('userId');
        $paf_id = json_decode($paf->getMatterIdAttr($user_id))->paf_id;
        $check = $request->param('check');
        $data = ['paf.check' => $check];
        $paf->where('paf_id','=',$paf_id)->update($data);
        if ($check == 0) return $this->returnJson(self::OK,'此用户未通过申请人事');
        $pos = ['position' => 1];
        $user->where('user_id','=',$user_id)->update($pos);
         return $this->returnJson(self::OK,'成功申请人事');
    }

    /**
     * 获取人事手机号接口
     * @copyright hyw
     * @return \think\response\Json
     */
    public function maPhone() {
        return $this->returnJson(self::OK,'输出成功',$this->getUserPhone());
    }
}